package top.lgd.controller.person;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lgd.base.LgdConstants;
import top.lgd.base.LgdResult;
import top.lgd.base.LgdTable;
import top.lgd.controller.LgdController;
import top.lgd.enums.AccountTypeEnum;
import top.lgd.pojo.entity.person.Account;
import top.lgd.pojo.req.person.AccountOpReq;
import top.lgd.service.person.AccountService;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/19 19:12
 */
@RestController
@RequestMapping("/admin/personAccount")
public class AccountAdminController extends LgdController {

    @Autowired
    private AccountService accountService;

    @GetMapping("queryListByType")
    public LgdTable<Account> queryListByType(String accountType) {
        return getTable(accountService.queryAccountList(current().getId(), accountType));
    }

    /**
     * 查询 个人账户 列表
     *
     * @param account 账户表查询条件
     * @return
     */
    @GetMapping("queryList")
    public LgdTable<Account> queryList(Account account) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper(account);
        queryWrapper.eq(LgdConstants.COLUMN_DATA_USER_ID, current().getId())
                .gt(LgdConstants.COLUMN_STATUS, 0);
        if (AccountTypeEnum.claim.name().equals(account.getAccountType()) ||
                AccountTypeEnum.debt.name().equals(account.getAccountType())) {
            queryWrapper.orderByDesc(LgdConstants.COLUMN_BALANCE);
        } else {
            queryWrapper.orderByAsc(LgdConstants.COLUMN_BILL_FLAG, LgdConstants.COLUMN_SORT_NUM);
        }
        Page<Account> pageInfo = accountService.page(new Page(account.getCurrent(), account.getPageSize()), queryWrapper);
        return getTable(pageInfo);
    }

    /**
     * 添加 个人账户
     *
     * @param account 个人账户 对象
     * @return
     */
    @PostMapping("add")
    public LgdResult<Account> add(@RequestBody Account account) {
        String accountAttr = AccountTypeEnum.getAttr(account.getAccountType());
        if (null == accountAttr) {
            return LgdResult.fail("账户类型错误");
        }
        account.setAccountAttr(accountAttr);
        account.setDataUserId(current().getId());

        if (accountService.addAccountAndFlow(account)) {
            return LgdResult.success(account);
        } else {
            return LgdResult.fail("添加个人账户 失败");
        }
    }

    /**
     * 更新 个人账户
     *
     * @param accountOpReq 个人账户 对象
     * @return
     */
    @PostMapping("update")
    public LgdResult<Account> update(@RequestBody AccountOpReq accountOpReq) {
        Account account = accountService.queryAccount(accountOpReq.getId());
        if (null == account) {
            return LgdResult.fail("更新个人账户 失败");
        }

        if (accountService.updateAccountAndFlow(account, accountOpReq)) {
            return LgdResult.success(account);
        } else {
            return LgdResult.fail("更新个人账户 失败");
        }
    }
}